package cn.com.flower.admin.content.service.impl;

import cn.com.flower.admin.content.dao.persist.repository.IArticleDetailRepository;
import cn.com.flower.admin.content.dao.persist.repository.IArticleRepository;
import cn.com.flower.admin.content.dao.persist.repository.ICategoryRepository;
import cn.com.flower.admin.content.pojo.entity.Article;
import cn.com.flower.admin.content.pojo.entity.ArticleDetail;
import cn.com.flower.admin.content.pojo.param.ArticleAddNewParam;
import cn.com.flower.admin.content.pojo.vo.CategoryStandardVO;
import cn.com.flower.admin.content.service.IArticleService;
import cn.com.flower.common.enumerator.ServiceCode;
import cn.com.flower.common.ex.ServiceException;
import cn.com.flower.common.pojo.authentication.CurrentPrincipal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private IArticleDetailRepository articleDetailRepository;
    @Autowired
    private ICategoryRepository categoryRepository;
    @Autowired
    private IArticleRepository articleRepository;
    @Override
    public void addNew(CurrentPrincipal currentPrincipal, String remoteAddr, ArticleAddNewParam articleAddNewParam) {
        log.debug("开始处理【发布文章】的业务，当事人：{}，IP地址：{},参数：{}",currentPrincipal,remoteAddr,articleAddNewParam);

        Long categoryId=articleAddNewParam.getCategoryId();
        CategoryStandardVO category = categoryRepository.getStandardById(categoryId);
        if(category==null){
            String message="发布文章失败，选择的类别不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        if(category.getEnable()!=1){
            String message="发布文章失败，选择的类别已经被禁用!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }

        Article article=new Article();
        BeanUtils.copyProperties(articleAddNewParam,article);
        article.setAuthorId(currentPrincipal.getId());
        article.setAuthorName(currentPrincipal.getUsername());
        article.setCategoryName(category.getName());
        article.setIp(remoteAddr);
        article.setClickCount(0);
        article.setUpCount(0);
        article.setDownCount(0);
        article.setCheckState(CHECK_STATE_UNCHECKED);
        article.setIsDisplay(DISPLAY_STATE_OFF);
        article.setIsRecommend(RECOMMEND_STATE_OFF);
        int rows=articleRepository.insert(article);
        if(rows!=1){
            String message="发布文章失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
        ArticleDetail articleDetail=new ArticleDetail();
        articleDetail.setArticleId(article.getId());
        articleDetail.setDetail(articleAddNewParam.getDetail());
        rows=articleDetailRepository.insert(articleDetail);
        if(rows!=1){
            String message="发布文章失败，服务器忙，请稍后在尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }

    }
}
